package lgkj.meicqt20.utils;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by Administrator on 2018/12/26 0026.
 */

public class CommUtils {

    public static boolean isAutoLevel = true;

    public static boolean isMp4(String name) {
        return (name.indexOf(".mp4") != -1) ? true : false;
    }
    //根据Wifi信息获取本地Mac
    public static String getLocalMacAddressFromWifiInfo(Context context) {
        if (context == null) {
            return "64:cc:2e:fc:ed:fd";
        }
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        String mac = info.getMacAddress();
        Log.i("information", mac + "");
//        return "10:a4:be:98:4a:e4";
        if (mac != null && mac.length() > 0) {
            if (mac.contains("00:00:00")) {
                return "64:cc:2e:fc:ed:fd";
            } else {
                return mac;
            }
        } else {
            return "64:cc:2e:fc:ed:fd";
        }
    }

    /**
     * @param context
     * @param imageView
     * @param url
     * @param errImagId
     */
    public static void LoadImage(Context context, ImageView imageView, String url, int errImagId) {
        if (context == null) {
            Log.e("Glide.load", "context  is  null: ");
            return;
        }
        if (url == null) {
            Log.e("Glide.load", "url  is  null: ");
            return;
        }
        if (url.contains("http:") || url.contains("https:")) {
        } else {
            url = "http:" + url;
        }
        Log.d("Glide.load", "LoadImage: " + url);
        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).error(errImagId).into(imageView);
    }


    public static boolean startPing() {
        try {
            Process p = Runtime.getRuntime().exec("ping -c 1 -w 5 www.baidu.com", null, null);
            int status = p.waitFor();
            if (status == 0) {
                return true;
            } else {
                return false;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            return false;
        }
    }

    public static void closeBar(Activity a) {


        int uiOptions = a.getWindow().getDecorView().getSystemUiVisibility();
        int newUiOptions = uiOptions;
        // END_INCLUDE (get_current_ui_flags)
        // BEGIN_INCLUDE (toggle_ui_flags)
        boolean isImmersiveModeEnabled =
                ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
        if (isImmersiveModeEnabled) {
            Log.i("123", "Turning immersive mode mode off. ");
        } else {
            Log.i("123", "Turning immersive mode mode on.");
        }

        // Navigation bar hiding:  Backwards compatible to ICS.
        if (Build.VERSION.SDK_INT >= 14) {
            newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        }

        // Status bar hiding: Backwards compatible to Jellybean
        if (Build.VERSION.SDK_INT >= 16) {
            newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
        }

        // Immersive mode: Backward compatible to KitKat.
        // Note that this flag doesn't do anything by itself, it only augments the behavior
        // of HIDE_NAVIGATION and FLAG_FULLSCREEN.  For the purposes of this sample
        // all three flags are being toggled together.
        // Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
        // Sticky immersive mode differs in that it makes the navigation and status bars
        // semi-transparent, and the UI flag does not get cleared when the user interacts with
        // the screen.
        if (Build.VERSION.SDK_INT >= 18) {
            newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        }

        //a.getWindow().getDecorView().setSystemUiVisibility(newUiOptions);//上边状态栏和底部状态栏滑动都可以调出状态栏
        a.getWindow().getDecorView().setSystemUiVisibility(4108);//这里的4108可防止从底部滑动调出底部导航栏
        //END_INCLUDE (set_ui_flags)
    }

    /**
     * 生成32位md5码
     *
     * @param password
     * @return
     */
    public static String md5Password(String password) {

        try {
            // 得到一个信息摘要器
            MessageDigest digest = MessageDigest.getInstance("md5");
            byte[] result = digest.digest(password.getBytes());
            StringBuffer buffer = new StringBuffer();
            // 把每一个byte 做一个与运算 0xff;
            for (byte b : result) {
                // 与运算
                int number = b & 0xff;// 加盐
                String str = Integer.toHexString(number);
                if (str.length() == 1) {
                    buffer.append("0");
                }
                buffer.append(str);
            }

            // 标准的md5加密后的结果
            return buffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        }
    }
}
